package com.luhy.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue orderTTLQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 设置队列中的消息等待的时间
        arguments.put("x-message-ttl", 5000);
        // 设置关联的死信交换器
        arguments.put("x-dead-letter-exchange", "ed");
        // 设置关联的死信队列路由Key
        arguments.put("x-dead-letter-routing-key", "rd");
        Queue queue = new Queue("qt", false, false, false, arguments);
        return queue;
    }

    @Bean
    public Exchange orderTTLExchange() {
        DirectExchange tk = new DirectExchange("et");
        return tk;
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(orderTTLQueue()).to(orderTTLExchange()).with("kt").noargs();
    }

    @Bean
    public Queue orderDeadLetterQueue() {
        return new Queue("qd");
    }

    @Bean
    public Exchange orderDeadLetterExchange() {
        return new DirectExchange("ed");
    }

    @Bean
    public Binding orderDeadLetterBinding() {
        return BindingBuilder.bind(orderDeadLetterQueue()).to(orderDeadLetterExchange()).with("rd").noargs();
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
       return  new RabbitAdmin(connectionFactory);
    }
}
